package com.sunshine.wrongquestionset.activity.me

import android.app.Application
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.LiveData
import com.sunshine.wrongquestionset.room.entity.AppConfigEntity
import com.sunshine.wrongquestionset.room.entity.UserEntity
import com.sunshine.wrongquestionset.room.repository.MyRepository

/**
 * @author sunshine
 * @date 2021/4/15
 */
class MeViewModel(application: Application) : AndroidViewModel(application) {
    private val repository: MyRepository = MyRepository(application.applicationContext)

    /**
     * 修改用户信息
     */
    fun updateAppConfig(appConfigEntity: AppConfigEntity) {
        repository.updateAppConfigByEntity(appConfigEntity)
    }

    fun getLoginUserEntity(userId: String): LiveData<UserEntity?> {
        return repository.getUser(userId)
    }

    fun updateLoginUserEntity(userEntity: UserEntity) {
        repository.updateUser(userEntity)
    }

    fun getUserById(userId: String): LiveData<UserEntity?> {
        return repository.getUser(userId)
    }

    fun updateUser(userEntity: UserEntity?) {
        if (userEntity != null) repository.updateUser(userEntity)
    }
}